# -*- coding: utf-8 -*-

from __future__ import with_statement

from flask import url_for
from dicthash.for_testing import captured_templates, TestFlask, TestGoogleDataStore

from user.code import register
from user.code.backends.working.models import User


class MyFlask(TestFlask):
    """ """
    TEMPLATES = {
        'user/view_user_data.html': """ 
            %user/view_user_data.html%
        """
    }
        
    
class TestBase(TestGoogleDataStore):
    """ """
    def setUp(self):
        """ """
        super(TestBase, self).setUp()
                
        self.app = MyFlask(__name__)
        self.app.config.update(
            SECRET_KEY = "12345"
        )
                
        register(self.app, '/user',
            {
                'user_name_validator': lambda name: True if name else False
            }
        )
    
    def tearDown(self):
        self.testbed.deactivate() 


class TestViewUser(TestBase):
    """ """
    def test_not_found(self):
        """ """
        res = self.app.test_client().get('/user/foo')
        self.assertEqual(res.status_code, 404)
        
    def test_found(self):
        """ """
        User.new('foo', 'foo@gmail.com').put()
        
        with captured_templates(self.app) as tmpl_info:
            res = self.app.test_client().get('/user/foo')
            self.assertEqual(res.status_code, 200)
            self.assertEqual(tmpl_info[0][0], "user/view_user_data.html")
            self.assertTrue('user_in_question' in tmpl_info[0][1])


class TestUrlFor(TestBase):
    """ """
    def test(self):
        with self.app.test_request_context('/user/foo'):
            url = url_for('user.view_user_data', username='foo')